<?php
class staticController
{
	function index()
	{
		$articles = ArticlesQuery::create()->orderByDateCreated("desc")->find();
		$this->display_articles($articles);
	}
	
	function search()
	{
		$textsQuery = TextsQuery::create();
		
		$query = explode(" ", $_GET["s"]);
		foreach($query as $item) {
			$textsQuery = $textsQuery->filterByLanguage(Language::get(), $item);
		}
		$texts = $textsQuery->find();
		
		$articles = array();
		foreach($texts as $text) {
			$articles[] = ArticlesQuery::create()->findPK($text->getPageId());
		}
		
		$this->display_articles($articles);
	}
	
	private function display_articles($articles) {
		$smarty = new BlogSmarty();
		
		global $driver;
		global $dbinfo;
		global $params;
		$tr =& Translation2::factory($driver, $dbinfo, $params);
		$tr->setLang(Language::get());
		
		$smarty_articles = array();
		
		
		foreach($articles as $article) {
			$items = $tr->getPage($article->getId());
			$updated_on = null;
			$date_created = new DateTime($article->getDateCreated());
			$created_on = $date_created->format('H:i:s, d-m-Y');
			if($article->getDateModified()) {
				$date_updated = new DateTime($article->getDateModified());
				$updated_on = $date_updated->format('H:i:s, d-m-Y');
			}
			
			$smarty_articles[] = array("id" => $article->getId(), "title" => $article->getTitle(), "content" => array_pop($items), "created_on" => $created_on, "updated_on" => $updated_on);
		}
		
		$smarty->assign("articles", $smarty_articles);
		
		$smarty->display("index.tpl");
	}
}
?>
